package com.example.manager.controller;

import com.example.manager.entity.*;
import com.example.manager.service.Imple.BookService;
import com.example.manager.utils.borrowPageBean;
import com.example.manager.utils.followPageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("book")
public class BookController {
    @Autowired
    BookService bookService;
//  日志
    Logger logger = LoggerFactory.getLogger(this.getClass().getName());


//    管理员图书管理页面
    @RequestMapping("showbook")
    public String showbooks(String page, Model model){
        int currentpage = 0;
        if (page == null){
            currentpage = 1;
        }else {
            currentpage = Integer.parseInt(page);
        }
        PageBean pageBean = new PageBean();
        pageBean = bookService.getBookByPage(currentpage,5);
        model.addAttribute("pageBean",pageBean);
        logger.info(pageBean.toString());


        return "adminshowbook";
    }

//    前往用户的图书信息界面
    @RequestMapping("showbook1")
    public String showbooks1(String page, Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        int currentpage = 0;
        if (page == null){
            currentpage = 1;
        }else {
            currentpage = Integer.parseInt(page);
        }
        PageBean pageBean = new PageBean();
        List followlist = new ArrayList();
        pageBean = bookService.getBookByPage(currentpage,5);

        followlist = bookService.checkFollowBooks(Integer.valueOf(user.username));
        model.addAttribute("followlist",followlist);
        model.addAttribute("pageBean",pageBean);
        logger.info(pageBean.toString());


        return "usershowbook";
    }

//    前往书籍详情界面
    @RequestMapping("bookedit")
    public String edit(Model model,int bookid){
        Book book = new Book();
        book = bookService.findBookById(bookid);
        model.addAttribute("book",book);
        logger.info(book.toString());
        return "bookedit";
    }


//    编辑图书
    @RequestMapping("editbook")
    public String bookedit1(Book book,Model model){
        logger.info(String.valueOf(book.getIsbn()));
        bookService.editBook(book);
        return "redirect:showbook";
    }
//      搜索图书
    @RequestMapping("searchbook")
    public String search(String page, Model model, String search, HttpSession session){
        User user = (User) session.getAttribute("user");
        int currentpage = 0;
        if (page == null){
            currentpage = 1;
        }else {
            currentpage = Integer.parseInt(page);
        }
        PageBean pageBean = new PageBean();
        pageBean = bookService.findBook(search,5);
        List followlist = new ArrayList();
        followlist = bookService.checkFollowBooks(Integer.valueOf(user.username));
        model.addAttribute("followlist",followlist);
        model.addAttribute("pageBean",pageBean);
        logger.info(pageBean.toString());


        if (user.getAdmin() == 1){
            return "adminshowbook";
        }else {
            return "usershowbook";
        }
    }
//      添加图书
    @RequestMapping("addbook")
    public String addbook(Book book){
        book.setImg("/img/book/default_book.jpg");
        bookService.addBook(book);
        return "redirect:showbook";
    }
//      删除图书
    @RequestMapping("delbook")
    public String delbook(Integer bookid, String page, RedirectAttributes redirectAttributes){
        bookService.delBook(bookid);
        redirectAttributes.addAttribute("page",page);
        return "redirect:showbook";
    }

//    用户借书功能
    @RequestMapping("borrowbook")
    public String borrowbook(Borrow borrow,String page,RedirectAttributes redirectAttributes){
        bookService.borrowBook(borrow);
        redirectAttributes.addAttribute("page",page);
        logger.info("page:"+page);
        return "redirect:showbook1";
    }

//    用户借书记录
    @RequestMapping("showborrow")
    public String showborrow(HttpSession session,String page, Model model){
        User user = (User) session.getAttribute("user");
        Integer username = Integer.valueOf(user.username);
        int currentpage = 0;
        if (page == null){
            currentpage = 1;
        }else {
            currentpage = Integer.parseInt(page);
        }
        borrowPageBean pageBean = new borrowPageBean();
        pageBean = bookService.getBorrowBooks(username,currentpage,5);
        model.addAttribute("pageBean",pageBean);
        logger.info(pageBean.toString());


        return "usershowborrow";
    }

//    管理员借还管理
    @RequestMapping("showborrow1")
    public String showborrow1(HttpSession session,String page, Model model){
        User user = (User) session.getAttribute("user");
        int currentpage = 0;
        if (page == null){
            currentpage = 1;
        }else {
            currentpage = Integer.parseInt(page);
        }
        borrowPageBean pageBean = new borrowPageBean();
        pageBean = bookService.getBorrowBooks1(currentpage,5);
        model.addAttribute("pageBean",pageBean);
        logger.info(pageBean.toString());


        return "adminshowborrow";
    }

//      还书功能
    @RequestMapping("backbook")
    public String backbook(Integer bookid,HttpSession session){
        User admin = (User) session.getAttribute("user");
        bookService.backBook(bookid);
        if (admin.getAdmin()==1){
            return "redirect:showborrow1";
        }


        return "redirect:showborrow";
    }

//      用户收藏
    @RequestMapping("followbook")
    public String followbook(Follow follow,String page,RedirectAttributes redirectAttributes){
        System.out.println(follow);
        bookService.followBook(follow);
        redirectAttributes.addAttribute("page",page);
        return "redirect:showbook1";
    }

//    取消收藏
    @RequestMapping("delfollowbook")
    public String delfollowbooks(Integer bookid){
        bookService.delFollowBook(bookid);
        return "redirect:showfollow";
    }

//    收藏分页
    @RequestMapping("showfollow")
    public String showfollow(HttpSession session,String page, Model model){
        User user = (User) session.getAttribute("user");
        int currentpage = 0;
        if (page == null){
            currentpage = 1;
        }else {
            currentpage = Integer.parseInt(page);
        }
        followPageBean pageBean = new followPageBean();
        List followlist = new ArrayList();
        pageBean = bookService.getFollowBooks(Integer.valueOf(user.username),currentpage,5);
        model.addAttribute("pageBean",pageBean);

        logger.info(pageBean.toString());

        return "showfollow";
    }
}
